import axios from 'src/utils/http'
import { reactive, ref, Ref } from 'vue'

interface PostArticleForm {
  title: string;
  description: string;
  body: string;
  tagList: string[]
}

const limit: number = 10

export async function getArticles (page: number = 1): Promise<ArticlesResponse> {
  const params = { limit, offset: (page - 1) * limit }
  const res = await axios.get('/articles', { params })
  return res.data
}

export async function postArticle (form: PostArticleForm): Promise<Article> {
  const res = await axios.post('/articles', JSON.stringify({ article: form }))
  return res.data.article
}

export async function getArticlesByUser (username: string, page: number = 1): Promise<ArticlesResponse> {
  const params = { limit, offset: (page - 1) * limit, author: username }
  const res = await axios.get('/articles', { params })
  return res.data
}

export async function getFavoritedArticles (username: string, page: number = 1): Promise<ArticlesResponse> {
  const params = { limit, offset: (page - 1) * limit, favorited: username }
  const res = await axios.get('/articles', { params })
  return res.data
}

export async function getArticleDetail (slug: string): Promise<Article> {
  const res = await axios.get(`/articles/${slug}`)
  return res.data.article
}
